package com.zegome.game.oanquan.scene.play;

import com.zegome.game.handler.IUpdateHandler;

import android.util.Log;

/**
 * (C) 2013 ZeroTeam
 * @author QuanLT
 */

public class CownDownClock implements IUpdateHandler{

	// ===========================================================
	// Constants
	// ===========================================================
	public static final int SYSTEM_MODE = 0;
	public static final int GAME_MODE = 1;
	
	// ===========================================================
	// Fields
	// ===========================================================
	private ITask mTask;
	
	private float mTimeToTask;
	private float mCountTime;
	
	//for SYSTEM_MODE
	private long mLastTime = 0;
	
	private int mMode;
	
	private final boolean isLoop;
	private boolean isFinish;
	private boolean isPause;
	
	// ===========================================================
	// Constructor
	// ===========================================================
	public CownDownClock(final float time, final boolean isLoop, final int mode) {
		this(time, isLoop, mode, null);
	}
	
	public CownDownClock(final float time, final boolean isLoop, final int mode, final ITask task) {
		this.isLoop = isLoop;
		mTimeToTask = time;
		mMode = mode;
		mCountTime = mTimeToTask;
		isFinish = false;
		isPause = false;
		mTask = task;
	}
	
	// ===========================================================
	// Getter & Setter
	// ===========================================================
	public float getCurrentCownDownTime() {
		return mCountTime;
	}
	
	public float getTimeTask() {
		return mTimeToTask;
	}
	
	public boolean isLoop() {
		return isLoop;
	}
	
	public int getMode() {
		return mMode;
	}
	
	public void setMode(final int mode) {
		mMode = mode;
	}
	
	public void setTimeTask(final float time) {
		this.mTimeToTask = time;
	}
	
	public void setTask(final ITask task) {
		if (task == null) return;
		mTask = task;
	}
	
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	@Override
	public void onUpdate(final float delta) {
		// TODO Auto-generated method stub
		if (isPause) {
			return;
		}
		
		if (mCountTime <= 0) {
			if (!isLoop) {
				isFinish = true;
			} else {
				mCountTime = mTimeToTask;
			}
			mTask.onTask();
		}
		
		if (mMode == SYSTEM_MODE) {
			final long currentTime = System.currentTimeMillis();
			if (mLastTime == 0) {
				mCountTime -= delta;
			} else {
				long deltaTime = currentTime - mLastTime;
				mCountTime -= (deltaTime/1000f);
			}
			mLastTime = currentTime;
		} else {
			mCountTime -= delta;
		}
	}

	@Override
	public void reset() {
		// TODO Auto-generated method stub
		mCountTime = mTimeToTask;
		isFinish = false;
		isPause = false;
	}

	@Override
	public boolean isDetachSelf() {
		// TODO Auto-generated method stub
		return isFinish;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	public void reStart() {
		isFinish = false;
		isPause = false;
		mCountTime = mTimeToTask;
	}
	
	public void resume() {
		isPause = false;
	}
	
	public void pause() {
		isPause = true;
	}
	
	public void finish() {
		isFinish = true;
	}
	
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
	public interface ITask {
		public void onTask();
	}
	
}
